package com.sky.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sky.exception.OrderBusinessException;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 获取两点之间的距离工具类
 *
 * @author wkh
 * @date 2024/6/20 16:10
 */
@Component
public class DistanceUtil {

    //地理编码接口地址
    public static final String ADDRESS = "https://api.map.baidu.com/geocoding/v3";
    //驾车路线规划接口地址
    public static final String DRIVE = "https://api.map.baidu.com/directionlite/v1/driving";
    //密钥
    public static final String AK = "zOZVPQAV0hB2UiQRGcbMuHpOAVmuAo5i";
    // 地球半径，单位千米
    private static final double EARTH_RADIUS = 6371.0;

    public static double getDistance(String shopAddress, String userAddress) {
        //获取商家店铺地址
        Map<String, String> map = new HashMap<>();
        map.put("address", shopAddress);
        map.put("output", "json");
        map.put("ak", AK);
        String address = HttpClientUtil.doGet(ADDRESS, map);

        JSONObject jsonObject = JSON.parseObject(address);
        if (!jsonObject.getString("status").equals("0")) {
            throw new OrderBusinessException("商家地址解析失败");
        }

        //数据解析
        JSONObject location = jsonObject.getJSONObject("result").getJSONObject("location");
        String shopLat = String.format("%.6f", Double.parseDouble(location.getString("lat")));
        String shopLng = String.format("%.6f", Double.parseDouble(location.getString("lng")));

        map.put("address", userAddress);
        address = HttpClientUtil.doGet(ADDRESS, map);
        jsonObject = JSON.parseObject(address);
        if (!jsonObject.getString("status").equals("0")) {
            throw new OrderBusinessException("用户地址解析失败");
        }

        //数据解析
        location = jsonObject.getJSONObject("result").getJSONObject("location");
        String userLat = String.format("%.6f", Double.parseDouble(location.getString("lat")));
        String userLng = String.format("%.6f", Double.parseDouble(location.getString("lng")));

        Map<String, String> map1 = new HashMap<>();
        String origin = shopLat + "," + shopLng;
        String destination = userLat + "," + userLng;
        map1.put("origin", origin);
        map1.put("destination",destination);
        map1.put("ak", AK);
        map1.put("steps_info", "0");
        String dis = HttpClientUtil.doGet(DRIVE, map1);
        JSONObject object = JSON.parseObject(dis);
        JSONObject routes = object.getJSONObject("result").getJSONArray("routes").getJSONObject(0);
        return routes.getDouble("distance");

//        double v = calculateDistance(Double.parseDouble(shopLat), Double.parseDouble(shopLng), Double.parseDouble(userLat), Double.parseDouble(userLng));
//        return (int) (v * 1000);
    }


    /**
     * 计算两个地点之间的距离（单位：千米）
     * @param lat1 第一个地点的纬度
     * @param lon1 第一个地点的经度
     * @param lat2 第二个地点的纬度
     * @param lon2 第二个地点的经度
     * @return 距离，单位千米
     */
    public static double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
        // 将经纬度转换为弧度
        double radLat1 = Math.toRadians(lat1);
        double radLon1 = Math.toRadians(lon1);
        double radLat2 = Math.toRadians(lat2);
        double radLon2 = Math.toRadians(lon2);

        // 计算纬度和经度的差值
        double deltaLat = radLat2 - radLat1;
        double deltaLon = radLon2 - radLon1;

        // 应用哈弗辛公式
        double a = Math.pow(Math.sin(deltaLat / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(deltaLon / 2), 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        // 计算最终距离
        return EARTH_RADIUS * c;
    }
}
